$().ready(function(){
	
	var url;
	/**
	 * click newObject button to trigger a function to open form
	 */
	$("#commodityClass-newObject").click(function(){
		$('#commodityClass-dlg').dialog('open').dialog('setTitle','添加');
		$('#commodityClass-saveObject').show("fast");
		$('#commodityClass-modifyObject').hide("fast");
		$('#commodityClass_fm').form('clear');
	});

	$("#commodityClass-editObject").click(function(){
		var row = $('#commodityClass-table').datagrid('getSelected');
		if (row){
			$('#commodityClass-dlg').dialog('open').dialog('setTitle','修改');
			$('#commodityClass-saveObject').hide("fast");
			$('#commodityClass-modifyObject').show("fast");
			$('#commodityClass_fm').form('load',row);
		}
	});
	/**
	 * save a new data of commodity class
	 */
	$("#commodityClass-saveObject").click(function(){
		$('#commodityClass_fm').form('submit',{
			url: "commodityClass_add.action",
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.errorMsg){
					$.messager.show({
						title: 'Error',
						msg: result.errorMsg
					});
				} else {
					$.messager.show({
						title:"Success",
						msg:"添加成功！"
					});
					$('#commodityClass-dlg').dialog('close'); // close the dialog
					$('#commodityClass-table').datagrid('reload'); // reload the user data
				}
			}
		});
	});
	/**
	 * modify a data of Commodity Class
	 */
	$("#commodityClass-modifyObject").click(function(){
		$('#commodityClass_fm').form('submit',{
			url: "commodityClass_modify.action",
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.errorMsg){
					$.messager.show({
						title: '修改失败',
						msg: result.errorMsg
					});
				} else {
					$.messager.show({
						title:"Success",
						msg:"修改成功！"
					});
					$('#commodityClass-dlg').dialog('close'); // close the dialog
					$('#commodityClass-table').datagrid('reload'); // reload the user data
				}
			}
		});
	});
	
	/**
	 * delete a data of Commodity Class
	 */
	$("#commodityClass-destroyObject").click(function(){
		var row = $('#commodityClass-table').datagrid('getSelected');
		if (row){
			$.messager.confirm('Confirm','确定删除改行数据?',function(r){
				if (r){
					$.post('commodityClass_delete.action',{commodityClassId:row.id},function(result){
						if (result){
							$('#commodityClass-table').datagrid('reload'); // reload the user data
							$.messager.show({ // show error message
								title: 'SUCCESS',
								msg: "删除成功"
							});
						} else {
							$.messager.show({ // show error message
								title: '删除失败',
								msg: result.errorMsg
							});
						}
					},'json');
				}
			});
		}
	});
});